package sk.demo.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Scanner;

/**
 * @author sk.z
 */
@Service
public class Console {

    @Autowired
    private MessageService messageService;

    public void cmd() {
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.print("cmd:>");
            while (scanner.hasNextLine()) {
                try {
                    String cmd = scanner.nextLine();
                    if ("save".equals(cmd)) {
                        messageService.save();
                    } else if ("select".equals(cmd)) {
                        messageService.select();
                    } else if ("time".equals(cmd)) {
                        System.out.println(new Date());
                    } else if ("exit".equals(cmd)) {
                        break;
                    } else {
                        System.out.println("un know cmd : " + cmd);
                    }
                } catch (Exception ignored) {
                }
                System.out.print("cmd:>");
            }
        }).start();
    }

    @EventListener
    public void onHandler(ApplicationReadyEvent event) {
        cmd();
    }
}
